//
//  NewfeatureViewController.swift
//  weibo1
//
//  Created by changhongliang on 2017/7/25.
//  Copyright © 2017年 richinfo. All rights reserved.
//

import UIKit

class NewfeatureViewController: UIViewController,UICollectionViewDataSource,UICollectionViewDelegate {
    
    ///页面数目
    let imageNum = 4
    let NewfeaturCollectionViewCellID = "NewfeaturCollectionViewCellID"

    override func viewDidLoad() {
        super.viewDidLoad()
        newFeatureCollection.dataSource = self
        newFeatureCollection.delegate = self
        view.addSubview(newFeatureCollection)
        newFeatureCollection.register(NewfeaturCollectionViewCell.self, forCellWithReuseIdentifier: NewfeaturCollectionViewCellID)
    }
    
    
    lazy var newFeatureCollection = UICollectionView(frame:UIScreen.main.bounds, collectionViewLayout: NewfeatureCollectionViewLayout())

  }


// MARK:- collectionView的数据源和代理方法
extension NewfeatureViewController{
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
        return imageNum
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell{
        let newFeatur = collectionView.dequeueReusableCell(withReuseIdentifier: NewfeaturCollectionViewCellID, for: indexPath)as! NewfeaturCollectionViewCell
        newFeatur.index = indexPath.item
        newFeatur.startButton.isHidden = true
        return newFeatur
    }
    
    //MARK: - UICollectionViewDelegate
    func collectionView(_ collectionView: UICollectionView, didEndDisplaying cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
        //注意:该方法传递给我们的是上一页的索引
        //1.获取当前展现在眼前的cell对应的索引
        let path = collectionView.indexPathsForVisibleItems.last!
        //2.根据索引获取当前展现在眼前的cell
        let cell = collectionView.cellForItem(at: path) as! NewfeaturCollectionViewCell
        if path.item == imageNum - 1  {
            cell.startButton.isHidden = false
            cell.startButton.isUserInteractionEnabled = false
            cell.startButton.transform = CGAffineTransform(scaleX: 0.0, y: 0.0)
            UIView.animate(withDuration: 0.5, animations: {
                cell.startButton.transform = CGAffineTransform.identity
            }, completion: { (_) in
                cell.startButton.isUserInteractionEnabled = true
            })
        }
        
        
    }
}

class NewfeaturCollectionViewCell: UICollectionViewCell {
    
    var index = 0 {
        didSet{
            imageView.image = UIImage(named: "new_feature_\(index+1)" )
        }
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加子控件
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
       super.init(coder: aDecoder)
       setupUI()
    }
    
    func setupUI(){
        self.contentView.addSubview(imageView)
        imageView.snp.makeConstraints { (make) in
            make.center.equalTo(self.contentView)
        }
        self.contentView.addSubview(startButton)
        startButton.snp.makeConstraints { (make) in
            make.centerX.equalTo(self.contentView)
            make.centerY.equalTo(self.contentView.snp.bottom).offset(-160)
        }
      
        
    }
    
    lazy var imageView = UIImageView()
    lazy var startButton:UIButton = {
        let btn = UIButton(imageName: "", backImageName: "new_feature_button")
        btn.addTarget(self, action: #selector(NewfeaturCollectionViewCell.startButtonClick), for:UIControlEvents.touchUpInside)
        return btn
    }()
    
    @objc private func startButtonClick() {
       NotificationCenter.default.post(name:NSNotification.Name(rawValue: HLSwitchMainInterfaceNotification), object: true)
    }
    
}

class NewfeatureCollectionViewLayout: UICollectionViewFlowLayout {
    override func prepare() {
        super.prepare()
        itemSize = (collectionView?.bounds.size)!
        minimumLineSpacing = 0
        minimumInteritemSpacing = 0
        scrollDirection = UICollectionViewScrollDirection.horizontal
        collectionView?.bounces = false
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.isPagingEnabled = true
    }
}
